package com.example.demo.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.demo.mapper.GradeMapper;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.mapper.TeacherMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.entity.Grade;
import com.example.demo.entity.Message;
import com.example.demo.entity.Teacher;
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("/teacher")
@Controller
public class TeacherController {
    @Autowired
    private  TeacherMapper tm;
    @Autowired
    private UserMapper um;
    @Autowired
    private  GradeMapper gradeMapper;
    @Autowired
    private  StudentMapper studentMapper;
    @RequestMapping("/getTeaInfo")
    public void getAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Teacher> list = new ArrayList<>();
        list = tm.findAllTea();
        if (list != null) {
            response.getWriter().write(JSONObject.toJSONString(Message.success(list)));
        } else response.getWriter().write(JSONObject.toJSONString(Message.failure("Select result is error")));
    }

    @RequestMapping("/getTeaByName")
    public void getTeaByName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        Teacher teaByName = tm.findTeaByName(name);
        if (teaByName != null) {
            response.getWriter().write(JSONObject.toJSONString(Message.success(teaByName)));
        } else response.getWriter().write(JSONObject.toJSONString(Message.failure("Select result is error")));
    }

    @RequestMapping("/deletetea")
    public void deleteByTid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int tid = Integer.valueOf(request.getParameter("tid"));
        Teacher teaByTid = tm.findTeaByTid(tid);
        int grade=Integer.valueOf(teaByTid.getGrade().split("级")[0]);
        Date date = new Date();
        SimpleDateFormat getYearFormat = new SimpleDateFormat("yyyy");
        int currentYear = Integer.valueOf(getYearFormat.format(date));
        if((currentYear-grade)<4) response.getWriter().write(JSONObject.toJSONString(Message.failure("Delete failure,because this teacher have student")));
        int n = tm.deleteByTid(tid);
        if (n > 0) {
            response.getWriter().write(JSONObject.toJSONString(Message.success("Delete success")));
        } else response.getWriter().write(JSONObject.toJSONString(Message.failure("Delete failure")));
    }

    @RequestMapping("/addtea")
    public void addTea(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int tid = Integer.valueOf(request.getParameter("tid"));
        String tname = request.getParameter("tname");
        int sex = Integer.valueOf(request.getParameter("sex"));
        String college = request.getParameter("college");
        String grade = request.getParameter("grade");
        String tel = request.getParameter("tel");
        if (!"".equals(tname) && !"".equals(college) && !"".equals(grade) && !"".equals(tel) && tname != null && college != null && grade != null && tel != null) {
            List<Teacher> list = new ArrayList<>();
            list = tm.findAllTea();
            User u=um.findByname(tname);
            boolean isTidExist = list.stream().anyMatch(teacher -> teacher.getTid() == tid);
            Grade grade1 = gradeMapper.selectByCollegeAndGrade(college, grade);
            if(grade1==null) response.getWriter().write(JSONObject.toJSONString(Message.failure("Data error,because grade is not exist")));
            if (!isTidExist&&u!=null) {
                Teacher t = new Teacher(tid, tname, sex, college, grade, tel);
                int n = tm.addTea(t);
                if (n > 0) {
                    response.getWriter().write(JSONObject.toJSONString(Message.success("Add success")));
                } else response.getWriter().write(JSONObject.toJSONString(Message.failure("Add failure")));
            } else response.getWriter().write(JSONObject.toJSONString(Message.failure("The tid is exist or tname is not exist")));
        } else response.getWriter().write(JSONObject.toJSONString(Message.failure("Data error")));
    }

    @RequestMapping("/geteabyid")
    public void getTeaByTid(HttpServletRequest request,HttpServletResponse response) throws IOException {
        int tid;
        try {
            tid = Integer.valueOf(request.getParameter("tid"));
        } catch (NumberFormatException e) {
            response.getWriter().write(JSONObject.toJSONString(Message.failure("format error")));
            return;
        }
        Teacher t=tm.findTeaByTid(tid);
        if(t!=null){
            response.getWriter().write(JSONObject.toJSONString(Message.success(t)));
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("Select result is error")));
    }

    @RequestMapping("/updatetea")
    public void updateTea(HttpServletRequest request,HttpServletResponse response) throws IOException {
        int tid = Integer.valueOf(request.getParameter("tid"));
        String tname = request.getParameter("tname");
        int sex = Integer.valueOf(request.getParameter("sex"));
        String college = request.getParameter("college");
        String grade = request.getParameter("grade");
        String tel = request.getParameter("tele");
        if (!"".equals(tname) && !"".equals(college) && !"".equals(grade) && !"".equals(tel) && tname != null && college != null && grade != null && tel != null){
            Teacher teacher = tm.findTeaByTid(tid);
            Teacher needUpdateTeacher = tm.findTeaByCollegeAndGrade(college, grade);
            Grade grade1 = gradeMapper.selectByCollegeAndGrade(college, grade);
            if(teacher==null || grade1==null || needUpdateTeacher!=null) {
                response.getWriter().write(JSONObject.toJSONString(Message.failure("Data error,because grade is not exist")));
            } else {
                int n = tm.updateTea(tname, sex, college, grade, tel, tid);
                if (n > 0) {
                    response.getWriter().write(JSONObject.toJSONString(Message.success("update success")));
                } else response.getWriter().write(JSONObject.toJSONString(Message.failure("update failure")));
            }
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("Data error")));
    }

    @RequestMapping("/geteabycollege")
    public void getTeaByCollege(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String college = request.getParameter("college");
        List<Teacher> list = new ArrayList<>();
        list = tm.findTeaByCollege(college);
        if (list != null) {
            response.getWriter().write(JSONObject.toJSONString(Message.success(list)));
        } else response.getWriter().write(JSONObject.toJSONString(Message.failure("Select result is error")));
    }
}
